﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Runtime.InteropServices;

namespace WenXia
{
    public enum AnswerResult
	{
	    Right,Wrong,Unknown
	}
    class QuestionManager:DrawableGameComponent
    {
        List<Question> quests = new List<Question>();
        Question q;
        int width;
        int height;
        Vector2 qpos;
        Vector2 apos;
        Vector2 bpos;
        Vector2 cpos;
        Vector2 dpos;
        Texture2D backTex;
        SpriteBatch sb;
        int charPerLine = 20;
        Rectangle backRect;
        Rectangle aRect;
        Rectangle bRect;
        Rectangle cRect;
        Rectangle dRect;
        int index=0;
        SpriteFontX fontX;
        List<TextContainer> tContainer = new List<TextContainer>();
        public QuestionManager(Game game):base(game)
        {
            this.DrawOrder = 512;
            width = Game.Window.ClientBounds.Width;
            height = Game.Window.ClientBounds.Height;
            qpos = new Vector2(width / 2 - 250, height / 2 - 100);
            this.fontX = TextDrawer.fontX;
            
            
            StreamReader sr = File.OpenText("Quest.txt");
            while (!sr.EndOfStream)
            {
                q.QuestionText = sr.ReadLine();
                q.OptA = sr.ReadLine();
                q.OptB = sr.ReadLine();
                q.OptC = sr.ReadLine();
                q.OptD = sr.ReadLine();
                q.AnsOrdinal = int.Parse(sr.ReadLine());
                quests.Add(q);
            }
            sr.Close();
            
        }
        protected override void LoadContent()
        {
            backTex=Game.Content.Load<Texture2D>("textBack");
            sb = new SpriteBatch(Game.GraphicsDevice);
            base.LoadContent();
        }
        protected override void UnloadContent()
        {
            base.UnloadContent();
        }
        public void SelectQuestion(int index){
            this.index = index;
            this.Ask();
        }
        public bool NextQuestion()
        {
            index++;
            if (index > quests.Count - 1)
            {
                index = quests.Count - 1;
                return false;
            }
            this.Ask();
            return true;
        }
        public int currentQuestion()
        {
            return index;
        }
        public void Ask()
        {
            Question q = quests[index];
            TextContainer t = new TextContainer(TextHelper.Paragraph(q.QuestionText,charPerLine), qpos);
            apos = new Vector2(qpos.X, qpos.Y + q.QuestionText.Length/charPerLine*30+30);
            bpos = new Vector2(apos.X, apos.Y + 30);
            cpos = new Vector2(apos.X, bpos.Y + 30);
            dpos = new Vector2(apos.X, cpos.Y + 30);
            Vector2 v=fontX.MeasureString(q.OptA);
            aRect = new Rectangle((int)apos.X, (int)apos.Y, (int)v.X,(int)v.Y);
            v = fontX.MeasureString(q.OptB);
            bRect = new Rectangle((int)bpos.X, (int)bpos.Y, (int)v.X, (int)v.Y);
            v = fontX.MeasureString(q.OptC);
            cRect = new Rectangle((int)cpos.X, (int)cpos.Y, (int)v.X, (int)v.Y);
            v = fontX.MeasureString(q.OptD);
            dRect = new Rectangle((int)dpos.X, (int)dpos.Y, (int)v.X, (int)v.Y);
            backRect = new Rectangle((int)qpos.X - 25, (int)qpos.Y - 25, charPerLine * 30 , (int)(dpos.Y - qpos.Y) + 80);
            tContainer.Add(t);
            t = new TextContainer(q.OptA, apos);
            tContainer.Add(t);
            t = new TextContainer(q.OptB, bpos);
            tContainer.Add(t);
            t = new TextContainer(q.OptC, cpos);
            tContainer.Add(t);
            t = new TextContainer(q.OptD, dpos);
            tContainer.Add(t);
        }
        public AnswerResult GetAnswerResult(int x, int y)
        {
            Question q = quests[index];
            int selected=int.MaxValue;
            if (aRect.Contains(x, y))
                selected = 0;
            else if (bRect.Contains(x, y))
                selected = 1;
            else if (cRect.Contains(x, y))
                selected = 2;
            else if (dRect.Contains(x, y))
                selected = 3;
            if(selected==int.MaxValue)
                return AnswerResult.Unknown;
            if(q.AnsOrdinal==selected)
                return AnswerResult.Right;
            else
                return AnswerResult.Wrong;
        }
        public override void Draw(GameTime gameTime)
        {
            sb.Begin();
            sb.Draw(backTex, backRect, Color.White);
            foreach (TextContainer t in this.tContainer)
            {
                fontX.Draw(sb, t.Text, t.Position, Color.Black);
            }
            sb.End();
            base.Draw(gameTime);
        }
    }
}
